<?
	class Beneficiary extends CI_Controller
	{
		public function __construct()
		{
			parent::__construct();
			$this->load->helper('url');
			if($this->input->post('cancel')) 
				redirect('Beneficiary/read');
			$this->load->library('session');
			$this->load->model('BeneficiaryModel');
		}
		private final function showView($fileName, $data)
		{
			$l = $this->load;
			$l->view('Templates/header.php', $data);
			$l->view('Beneficiaries/' . $fileName . '.php', $data);
			$l->view('Templates/footer.php', $data);
		}
		private final function getPostData()
		{
			$i = $this->input;
			$b = $this->BeneficiaryModel;
			$b->Id = $i->post('id');
			$b->FullName = $i->post('fullName');
			$b->Needs = $i->post('needs');
			$b->Conditions = $i->post('conditions');
			$b->NeededDonation = $i->post('neededDonation');
			$b->CurrentDonation = $i->post('currentDonation');
			$b->EventId = $i->post('eventId');
			$b->NgoId = $i->post('ngoId');
			return $b;
		}
		public final function create()
		{
			$data['title'] = 'Add a beneficiary';
			if($this->input->post('save'))
			{
				$id = $this->BeneficiaryModel->addBeneficiary($this->getPostData());
				$data['beneficiaries'] = $this->BeneficiaryModel->getBeneficiaryById($id);
				$this->showView('read', $data);
			}
			else
			{
				$this->showView('create', $data);
			}
		}
		public final function read($id = null)
		{
			if($id == null)
			{
				$data['title'] = 'View all beneficiaries';
				$data['beneficiaries'] = $this->BeneficiaryModel->getAllBeneficiaries();
			}
			else
			{
				$data['title'] = 'View beneficiary details';
				$data['beneficiaries'] = $this->BeneficiaryModel->getBeneficiaryById($id);
			}
			$this->showView('read', $data);
		}
		public final function update()
		{
			$data['title'] = 'Update a beneficiary';
			$data['id'] = $this->input->post('id');
			$this->BeneficiaryModel->updateBeneficiary($this->getPostData());
			$this->showView('update',  $data);
		}
		public final function edit($id)
		{
			$data['title'] = 'Update a beneficiary';
			$data['beneficiaries'] = $this->BeneficiaryModel->getBeneficiaryById($id);
			$this->showView('edit', $data);
		}
		public final function delete($id)
		{
			$data['title'] = 'Remove a beneficiary';
			$data['beneficiaries'] = $this->BeneficiaryModel->getBeneficiaryById($id);
			$this->BeneficiaryModel->removeBeneficiaryById($id);
			$this->showView('delete', $data);
		}
	}